﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using Sy.DbHelperA;
using System.Data.Common;


using System.Net.Mail;
using Microsoft.Exchange.WebServices;
using Microsoft.Exchange.WebServices.Data;
using System.Web;
using System.IO;

namespace EmailDS
{
    class MAGGZ
    {
        DbHelperA db = new DbHelperA();
        DbCommand cmd;

        public void setMAGGZ()
        {

            string sql = "exec SP_KD_DBICStokBILLJX";
            cmd = db.GetSqlStringCommand(sql);
            DataTable dt = db.ExecuteDataTable(cmd);
            if (dt.Rows.Count != 0)
            {
                SendMailUse(MailMsg(dt), "近效期物料提醒");
            }

            sql = "exec SP_KD_DBICStokBILLSX";

            cmd = db.GetSqlStringCommand(sql);
            dt = db.ExecuteDataTable(cmd);
            if (dt.Rows.Count != 0)
            {
                SendMailUse(MailMsg(dt), "失效期物料提醒");
            }


            sql = "exec SP_KD_SCICStokBILL ";
            cmd = db.GetSqlStringCommand(sql);
            db.ExecuteScalar(cmd);
        }
        private  static void SendMailUse(string text, string FZT)
        {

            string host = "smtprelay1.thermofisher.com";// 邮件服务器smtp.163.com表示网易邮箱服务器     smtp.exmail.qq.com   smtprelay1.thermofisher.com
            string userName = "k3@thermofisher.com";// 发送端账号   k3@thermofisher.com   zhichao.wang@ipoodee.com
            string password = "";// 发送端密码(这个客户端重置后的密码) 

            SmtpClient client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式    
            client.Host = host;//邮件服务器
            client.UseDefaultCredentials = true;
            client.Credentials = new System.Net.NetworkCredential(userName, password);//用户名、密码

            //////////////////////////////////////
            string strfrom = userName;


            string subject = FZT;//邮件的主题            


            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.From = new MailAddress(strfrom, "k3");

            //msg.To.Add("raisersc2@outlook.com");
            foreach (string str in System.IO.File.ReadAllLines("C:\\K3\\MailMAGGZ.txt", Encoding.Default))
            {
                msg.To.Add(str);

            }



            msg.Subject = subject;//邮件标题   
            msg.Body = text;//邮件内容      
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码   
            msg.IsBodyHtml = true;//是否是HTML邮件   
            msg.Priority = MailPriority.High;//邮件优先级   


            try
            {
                client.Send(msg);

                // MessageBox.Show("发送成功","邮件提醒");

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message + "发送邮件出错", "邮件提醒");

                //sql = "update workflow_file set FIndex=" + FIndex + "  where fileno ='" + fileno + "'";
                //cmd = db.GetSqlStringCommand(sql);
                //db.ExecuteScalar(cmd);

                Console.WriteLine(ex.Message, "发送邮件出错");
                Environment.Exit(0);

            }
        }

        private static string MailMsg(DataTable data)
        {
            string MailBody = "<p style=\"font-size: 10pt\">以下内容为系统自动发送，请勿直接回复，谢谢。</p><table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" bgcolor=\"000000\" style=\"font-size: 10pt;line-height: 15px;\">";
            MailBody += "<div align=\"center\">";
            MailBody += "<tr>";
            for (int hcol = 0; hcol < data.Columns.Count; hcol++)
            {
                MailBody += "<td bgcolor=\"999999\">&nbsp;&nbsp;&nbsp;";
                MailBody += data.Columns[hcol].ColumnName;
                MailBody += "&nbsp;&nbsp;&nbsp;</td>";
            }
            MailBody += "</tr>";

            for (int row = 0; row < data.Rows.Count; row++)
            {
                MailBody += "<tr>";
                for (int col = 0; col < data.Columns.Count; col++)
                {
                    MailBody += "<td bgcolor=\"dddddd\">&nbsp;&nbsp;&nbsp;";
                    MailBody += data.Rows[row][col].ToString();
                    MailBody += "&nbsp;&nbsp;&nbsp;</td>";
                }
                MailBody += "</tr>";
            }
            MailBody += "</table>";
            MailBody += "</div>";

            return MailBody;
        }

    }
}
